package com.bawei.es.controller;

import com.bawei.common.core.domain.R;
import com.bawei.domain.query.DocDeleteReq;
import com.bawei.domain.query.DocQueryReq;
import com.bawei.domain.request.DocInfoReq;
import com.bawei.domain.request.DocUpdReq;
import com.bawei.es.service.EsDocService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Log4j2
@RequestMapping("/es/doc")
public class EsDocController {

    @Autowired
    private EsDocService esDocService;

    /**
     * 创建文档
     * @param docInfoReq
     * @return
     */
    @PostMapping("/createDoc")
    public R createDoc(@RequestBody DocInfoReq docInfoReq){
        esDocService.createDoc(docInfoReq);
        return R.ok();
    }


    /**
     * 删除文档
     * @param id
     * @param indexName
     * @return
     */
    @PostMapping("/deleteDoc")
    public  R deleteDoc(@RequestParam String id,@RequestParam String indexName){
        esDocService.deleteDoc(id,indexName);
        return R.ok();
    }


    /**
     * ??
     * @param docQueryReq
     * @return
     */
    @PostMapping("/queryDoc")
    public  R queryDoc(@RequestBody DocQueryReq docQueryReq){
        Object o = esDocService.queryDoc(docQueryReq);
        return R.ok(o);
    }

    /**
     *查询文档
     * @param docQueryReq
     * @return
     */
    @PostMapping("/selectDoc")
    public R<List<Map<String,Object>>> selectDoc(@RequestBody DocQueryReq docQueryReq){
        List<Map<String, Object>> maps = esDocService.selectDoc(docQueryReq);
        return R.ok(maps);

    }


    /**
     * 修改
     * @param docUpdReq
     * @return
     */
    @PostMapping("/updateDoc")
    public  R updateDpc(@RequestBody DocUpdReq docUpdReq){
        esDocService.updateDoc(docUpdReq);
        return R.ok();
    }

    /**
     * 删除
     * @param docDeleteReq
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("根据ID删除文档")
    public R delete(@RequestBody DocDeleteReq docDeleteReq){
        esDocService.deleteDocs(docDeleteReq);
        return R.ok();
    }

}
